﻿'
' Quiz Module for DotNetNuke
' Project Contributors - Will Strohl (http://www.WillStrohl.com)
'
'Copyright (c) 2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Quiz Module for DotNetNuke, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Security
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Security.PortalSecurity
Imports DotNetNuke.UI.Skins.Skin
Imports WillStrohl.Modules.Quiz.QuizController
Imports WillStrohl.Modules.Quiz.QuizInfoMembers

Namespace WillStrohl.Modules.Quiz

    Partial Public MustInherit Class EditQuiz
        Inherits WNSPortalModuleBase

#Region " Private Members "

        Private p_QuizId As Integer = Null.NullInteger

#End Region

#Region " Public Properties "

        Private ReadOnly Property QuizId() As Integer
            Get
                If p_QuizId = Null.NullInteger Then
                    Dim obj As Object = Request.Params(QUERYSTRING_QUIZ_ID_KEY)
                    If Not obj Is Nothing AndAlso IsNumeric(obj) Then
                        p_QuizId = Integer.Parse(obj.ToString(), Globalization.NumberStyles.Integer)
                    End If
                End If

                Return p_QuizId
            End Get
        End Property

#End Region

#Region " Event Handlers "

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                If Not Me.Page.IsPostBack Then
                    Me.BindData()
                End If
            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc, Me.IsEditable)
            End Try
        End Sub

        Private Sub cmdCancel_Click(sender As Object, e As System.EventArgs) Handles cmdCancel.Click
            SendToQuizView()
        End Sub

        Private Sub cmdDelete_Click(sender As Object, e As System.EventArgs) Handles cmdDelete.Click
            DeleteQuiz()
            SendToQuizView()
        End Sub

        Private Sub cmdUpdate_Click(sender As Object, e As System.EventArgs) Handles cmdUpdate.Click
            If Page.IsValid Then
                SaveQuiz()
                SendToQuizView()
            End If
        End Sub

#End Region

#Region " Private Helper Functions "

        Private Sub BindData()

            Me.LocalizeModule()

            If QuizId > Null.NullInteger Then
                ' load the quiz from the database
                Dim ctlQuiz As New QuizController
                Dim oQuiz As QuizInfo = ctlQuiz.GetQuiz(QuizId)

                txtQuizName.Text = oQuiz.QuizName
                txtQuizDescription.Text = oQuiz.QuizDescription
                chkRandomizeQuestions.Checked = oQuiz.RandomizeQuestions
                If oQuiz.QuestionCount > Null.NullInteger Then
                    txtQuestionCount.Text = oQuiz.QuestionCount.ToString
                End If
                txtStartDate.SelectedDate = oQuiz.BeginDate
                If oQuiz.EndDate > Null.NullDate Then
                    txtEndDate.SelectedDate = oQuiz.EndDate
                End If
            Else
                ' set default values
                txtStartDate.SelectedDate = DateTime.Now
            End If

        End Sub

        Private Sub LocalizeModule()

            rfvQuizName.ErrorMessage = GetLocalizedString("rfvQuizName.ErrorMessage")
            rfvQuizDescription.ErrorMessage = GetLocalizedString("rfvQuizDescription.ErrorMessage")
            revQuestionCount.ErrorMessage = GetLocalizedString("revQuestionCount.ErrorMessage")
            rfvStartDate.ErrorMessage = GetLocalizedString("rfvStartDate.ErrorMessage")

            cmdCancel.Text = GetLocalizedString("cmdCancel.Text")
            cmdDelete.Text = GetLocalizedString("cmdDelete.Text")
            cmdUpdate.Text = GetLocalizedString("cmdUpdate.Text")

        End Sub

        Private Sub DeleteQuiz()
            Dim ctlQuiz As New QuizController
            ctlQuiz.DeleteQuiz(QuizId)
        End Sub

        Private Sub SaveQuiz()

            Dim sec As New PortalSecurity
            Dim ctlQuiz As New QuizController
            Dim oQuiz As QuizInfo = Nothing

            If QuizId > Null.NullInteger Then
                'update the existing quiz
                oQuiz = ctlQuiz.GetQuiz(QuizId)
            Else
                'create a new quiz
                oQuiz = New QuizInfo()
            End If

            oQuiz.BeginDate = txtStartDate.SelectedDate.Value
            If txtEndDate.SelectedDate.HasValue Then
                oQuiz.EndDate = txtEndDate.SelectedDate.Value
            Else
                oQuiz.EndDate = Null.NullDate
            End If
            oQuiz.LastModifiedBy = UserId
            oQuiz.PortalId = PortalId
            If Not String.IsNullOrEmpty(txtQuestionCount.Text) Then
                oQuiz.QuestionCount = Integer.Parse(sec.InputFilter(txtQuestionCount.Text, FilterFlag.NoMarkup), Globalization.NumberStyles.Integer)
            Else
                oQuiz.QuestionCount = Null.NullInteger
            End If
            oQuiz.QuizDescription = sec.InputFilter(txtQuizDescription.Text, FilterFlag.NoScripting)
            oQuiz.QuizName = sec.InputFilter(txtQuizName.Text, FilterFlag.NoMarkup)
            oQuiz.RandomizeQuestions = chkRandomizeQuestions.Checked

            ' save/update the quiz
            If QuizId > Null.NullInteger Then
                ctlQuiz.UpdateQuiz(oQuiz)
            Else
                p_QuizId = ctlQuiz.AddQuiz(oQuiz)
            End If

            ' set this quiz as the default quiz for the module
            If Settings(QUIZ_ID_FIELD) Is Nothing Then
                Dim ctlModule As New Entities.Modules.ModuleController
                ctlModule.UpdateTabModuleSetting(TabModuleId, QUIZ_ID_FIELD, QuizId.ToString())
                Entities.Modules.ModuleController.SynchronizeModule(ModuleId)
            End If

        End Sub

        Private Sub SendToQuizView()
            Response.Redirect(EditUrl(String.Empty, String.Empty, "List"))
        End Sub

        Private Sub HandleException(ByVal exc As Exception)
            LogException(exc)
            If Me.UserInfo.IsSuperUser Or Me.UserInfo.UserID = PortalSettings.AdministratorId Then
                AddModuleMessage(Me, _
                    String.Concat(exc.Message, "<br />", exc.StackTrace), _
                    Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
            Else
                AddModuleMessage(Me, exc.Message, _
                    Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
            End If
        End Sub

#End Region
        
    End Class

End Namespace